package com.prd.controller;

import com.prd.common.baseMapper.ApiResult;
import com.prd.model.Attendance;
import com.prd.service.AttendanceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.io.Serializable;
import java.util.List;


/**
 * 出勤打卡数据表控制层
 *
 * @author wuzhengchao
 * @since 2023-10-18 23:51:01
 */
@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    /**
     * 服务对象
     */
    @Resource
    private AttendanceService attendanceService;

    /**
     * 分页查询
     *
     * @param attendance 筛选条件
     * @return 查询结果
     */
    @PostMapping(value = "/queryByPage")
    public ApiResult queryByPage(@Validated @RequestBody Attendance attendance) {
        return this.attendanceService.queryByPage(attendance);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/queryById/{id}")
    public ApiResult queryById(@PathVariable("id") Serializable id) {
        return this.attendanceService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param attendance 实体
     * @return 新增结果
     */
    @PostMapping(value = "/add")
    public ApiResult add(@Validated @RequestBody Attendance attendance) {
        try {
            return this.attendanceService.insert(attendance);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

    /**
     * 编辑数据
     *
     * @param attendance 实体
     * @return 编辑结果
     */
    @PutMapping(value = "/update")
    public ApiResult update(@Validated @RequestBody Attendance attendance) {
        try {
            return this.attendanceService.update(attendance);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping(value = "/delete")
    public ApiResult delete(@RequestParam Serializable id) {
        try {
            return this.attendanceService.delete(id);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

    @PostMapping(value = "/deleteBatch")
    public ApiResult deleteBatch(@RequestBody List<Long> ids) {
        try {
            return this.attendanceService.deleteBatch(ids);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }

}

